#include <stdio.h>
#include <stdbool.h>

/**
 * @brief IO口简单滤波函数
 * 
 * @param tick_value 用来计时
 * @param filter_value 最终输出的值
 * @param timeout_tick 滤波的时长
 * @param io_status 输出的IO口状态
 */
void hall_filter_template(unsigned int *tick_value, unsigned int *filter_value, unsigned int timeout_tick, bool io_status)
{
    if(io_status == false)
    {
        if((*tick_value)++ >= timeout_tick)
        {
            *tick_value = timeout_tick;
            *filter_value = 1;
        }
    }
    else
    {
        (*tick_value) /= 2;
        if((*tick_value) <= 0)
        {
            (*tick_value) = 0;
            *filter_value = 0;
        }
    }
}
/**
 * @brief ADC均值滤波函数
 * 
 * @param buf 滤波缓存buf
 * @param adc_reg_value ADC输入值（16位）
 * @param ticks 用来计时
 * @param sum 用来保存buf的和
 * @param filter_size 滤波的长度
 * @return unsigned short int 滤波后的均值
 */
unsigned short int Adc_Filter_Template(unsigned short int *buf, unsigned short int adc_reg_value,unsigned int *ticks, unsigned int *sum,unsigned int filter_size)
{
    unsigned short int tmp = 0;

    tmp = buf[*ticks];//取出当前值
    buf[*ticks] = adc_reg_value;
    (*ticks)++;
    if(*ticks == filter_size){
        *ticks = 0;
    }
    *sum += adc_reg_value;
    *sum -= tmp;

    return (unsigned short int)((*sum)*1.00/filter_size);
}